package cn.italkcloud.cph.controller;

import cn.italkcloud.cph.config.sys.Response;
import cn.italkcloud.cph.entity.ReportVosCdr;
import cn.italkcloud.cph.entity.VosServer;
import cn.italkcloud.cph.entity.dto.vos.ReportCdrDTO;
import cn.italkcloud.cph.entity.dto.vos.ReportCustomerFeeDTO;
import cn.italkcloud.cph.entity.dto.vos.ReportGatewayFeeDTO;
import cn.italkcloud.cph.entity.req.GetReportVosCdrReq;
import cn.italkcloud.cph.entity.req.GetReportVosFeeReq;
import cn.italkcloud.cph.entity.wrapper.VosWrapper;
import cn.italkcloud.cph.helper.RequestCacheHelper;
import cn.italkcloud.cph.helper.VosHelper;
import cn.italkcloud.cph.service.IReportVosCdrService;
import cn.italkcloud.cph.service.IVosServerService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author dominate
 * @since 2025/3/19
 **/
@RestController
@RequestMapping("/report")
public class ReportController extends BaseController {

    @Resource
    private IVosServerService vosServerService;
    @Resource
    private IReportVosCdrService reportVosCdrService;

    @PostMapping("/getCustomerFee")
    public Response<List<ReportCustomerFeeDTO>> getReportCustomerFee(@RequestBody GetReportVosFeeReq req) {
        // 报表暂时不在接口做权限，页面上只提供可查询的列表就行
        if (RequestCacheHelper.hasReportCustomerFee(req)) {
            return Response.data(RequestCacheHelper.getReportCustomerFee(req));
        }
        String apiUrl = vosServerService.getApiUrl(req.getServerId());
        Date startDate = new Date(req.getStartDay());
        Date endDate = new Date(req.getEndDay());
        List<ReportCustomerFeeDTO> reportCustomerFeeList = VosHelper.getCustomerReport(apiUrl, startDate, endDate, req.getAccounts());
        RequestCacheHelper.saveReportCustomerFee(reportCustomerFeeList, req);
        return Response.data(reportCustomerFeeList);
    }

    @PostMapping("/getVosGatewayFee")
    public Response<List<ReportGatewayFeeDTO>> getVosGatewayFee(@RequestBody GetReportVosFeeReq req) {
        // 报表暂时不在接口做权限，页面上只提供可查询的列表就行
        if (RequestCacheHelper.hasReportVosGatewayFee(req)) {
            return Response.data(RequestCacheHelper.getReportVosGatewayFee(req));
        }
        VosServer server = vosServerService.getById(req.getServerId());
        Date startDate = new Date(req.getStartDay());
        Date endDate = new Date(req.getEndDay());
        List<ReportGatewayFeeDTO> reportGatewayFeeList = VosHelper.getGatewayReport(server, startDate, endDate, req.getAccounts());
        RequestCacheHelper.saveReportVosGatewayFee(reportGatewayFeeList, req);
        return Response.data(reportGatewayFeeList);
    }

    @PostMapping("/getReportVosCdr")
    public Response<List<ReportCdrDTO>> getReportVosCdr(@RequestBody GetReportVosCdrReq req) {
        List<ReportVosCdr> reportList = reportVosCdrService.getReport(req);
        return Response.data(VosWrapper.builder().entityReportCdrDTO(reportList));
    }
}
